<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch, computed } from 'vue';
import MessageNotificationModal from './MessageNotificationModal.vue';
import { useRouter, useRoute } from 'vue-router';
import { eventBus } from '../router';
import { API_CONFIG } from '../config/apiConfig';
import { useI18n } from 'vue-i18n';
const { t } = useI18n<{ message: typeof import('../locales/en-US').default }>();

const toggleLogoutMenu = () => {
  console.log('Before toggle - showLogoutMenu:', showLogoutMenu.value);
  showLogoutMenu.value = !showLogoutMenu.value;
  console.log('After toggle - showLogoutMenu:', showLogoutMenu.value);
  
  watch(showLogoutMenu, (newVal) => {
    console.log('showLogoutMenu changed to:', newVal);
  }, { immediate: true });
};

const router = useRouter();
const route = useRoute();
const showLogoutMenu = ref(false);
const showNotificationModal = ref(false);
const userNickname = ref('');
const userPermissions = ref<string[]>([]);
const userInfo = ref<any>(null);
const notifications = ref<Array<{
  id: number;
  type: string;
  content: string;
  read: boolean;
  unreadPop: number;
  createTime: string;
}>>([]);

const unreadCount = computed(() => {
  return notifications.value.filter(n => n.unreadPop < 3).length;
});

const fetchNotifications = async () => {
  try {
    const response = await fetch(`${API_CONFIG.BASE_URL}/message-notification/query`, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'include'
    });

    const result = await response.json();
    if (result.code === 0 || result.code === '0') {
      // 直接使用 result.data 数组，不再查找 dataList 属性
      if (result.data && Array.isArray(result.data)) {
        notifications.value = result.data;
        console.log('获取到的消息通知:', notifications.value);
        return true; // 成功获取消息
      } else {
        console.error('消息通知数据格式不正确:', result.data);
      }
    } else {
      console.error('获取消息通知失败:', result.msg);
    }
  } catch (error) {
    console.error('获取消息通知失败:', error);
  }
  return false; // 获取消息失败
};

const handleNotificationRead = (notification: any) => {
  const index = notifications.value.findIndex(n => n.id === notification.id);
  if (index !== -1) {
    notifications.value[index].read = true;
    notifications.value[index].unreadPop = 3;
  }
};

const toggleNotificationModal = () => {
  showNotificationModal.value = !showNotificationModal.value;
  showLogoutMenu.value = false;
};

// 加载用户信息的函数
const loadUserInfo = () => {
  const userInfoStr = localStorage.getItem('userInfo');
  if (userInfoStr) {
    const parsedInfo = JSON.parse(userInfoStr);
    userNickname.value = parsedInfo.nickname || t('common.user');
    userPermissions.value = parsedInfo.permissions || [];
    userInfo.value = parsedInfo;
  } else {
    userNickname.value = '';
    userPermissions.value = [];
    userInfo.value = null;
  }
};

// 添加检查权限的方法
const hasPermission = (permission: string | string[]): boolean => {
  if (userPermissions.value.includes('*')) {
    return true;
  }
  
  if (typeof permission === 'string') {
    return userPermissions.value.includes(permission);
  }
  
  return permission.every(p => userPermissions.value.includes(p));
};

// 监听路由变化
watch(() => route.path, async (newPath) => {
  loadUserInfo();
  showLogoutMenu.value = false;
  
  // 如果是从登录页面跳转到其他页面，不需要在这里获取消息
  // 登录成功后会通过 eventBus 触发获取消息
  if (newPath === '/') {
    showNotificationModal.value = false;
  }
});

// 监听localStorage变化的事件处理函数
const handleStorageChange = (event: StorageEvent) => {
  if (event.key === 'userInfo') {
    const userInfoStr = event.newValue;
    if (userInfoStr) {
      const parsedInfo = JSON.parse(userInfoStr);
      userNickname.value = parsedInfo.nickname || t('common.user');
      userPermissions.value = parsedInfo.permissions || [];
      userInfo.value = parsedInfo;
    } else {
      userNickname.value = '';
      userPermissions.value = [];
      userInfo.value = null;
    }
  }
};

// 处理点击外部关闭下拉菜单
const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement;
  const userInfoEl = document.querySelector('.user-info');
  const logoutMenuEl = document.querySelector('.logout-menu');
  
  if (userInfoEl && logoutMenuEl && 
      !userInfoEl.contains(target) && 
      !logoutMenuEl.contains(target)) {
    showLogoutMenu.value = false;
  }
};

// 组件挂载时添加事件监听
onMounted(() => {
  window.addEventListener('storage', handleStorageChange);
  document.addEventListener('click', handleClickOutside);
  loadUserInfo();
  // 只在登录后通过事件总线触发获取消息通知
  eventBus.on('fetch-notifications', async () => {
    await fetchNotifications();
    // 只要有 unreadPop < 3 的消息就显示通知窗口，不管是否已读
    if (unreadCount.value > 0) {
      showNotificationModal.value = true;
    }
  });
});

// 组件卸载时移除事件监听
onUnmounted(() => {
  window.removeEventListener('storage', handleStorageChange);
  document.removeEventListener('click', handleClickOutside);
  
  if (eventBus.listeners.has('fetch-notifications')) {
    const callbacks = eventBus.listeners.get('fetch-notifications') || [];
    const index = callbacks.indexOf(fetchNotifications);
    if (index > -1) {
      callbacks.splice(index, 1);
    }
  }
});

// 退出登录逻辑
const handleLogout = async () => {
  try {
    await fetch(`${API_CONFIG.BASE_URL}/user/logout`, {
      method: 'PUT',
      credentials: 'include'
    });
    localStorage.removeItem('userInfo');
    userNickname.value = '';
    document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
    showLogoutMenu.value = false;
    showNotificationModal.value = false;
    router.push('/');
  } catch (error) {
    console.error(t('app.logoutFailed'), error);
    alert(t('app.logoutRequestFailed'));
  }
}

function navigateTo(path: string) {
  router.push(path);
}
</script>

<template>
  <header class="app-header">
    <!-- 渐变背景 -->
    <div class="header-gradient"></div>
    
    <div class="header-content">
      <!-- 左侧品牌区域 -->
      <div class="brand-section">
        <div class="logo-container" @click="navigateTo('/')">
          <div class="logo-icon">
            <span class="ai-text">AI</span>
          </div>
          <div class="brand-info">
            <h1 class="brand-title">华师科研AI</h1>
            <span class="version-tag">V1.0 | 未来智能研究平台</span>
          </div>
        </div>
      </div>

      <!-- 中间导航区域 -->
      <nav class="main-navigation" v-if="userNickname">
        <div class="nav-tabs">
          <button 
          v-if="hasPermission(['chat'])"
            class="nav-tab" 
            :class="{ 'active': $route.path === '/chat' }"
            @click="navigateTo('/chat')"
          >
            <span class="tab-icon">🧠</span>
            <span class="tab-text">科研AI</span>
          </button>
          <button 
            v-if="hasPermission(['user'])"
            class="nav-tab" 
            :class="{ 'active': $route.path === '/users' }"
            @click="navigateTo('/users')"
          >
            <span class="tab-icon">👥</span>
            <span class="tab-text">用户管理</span>
          </button>
          <button 
            v-if="hasPermission(['knowledgeBase']) || (userInfo && ((userInfo.knowledgeBaseGroupRoleInfos && Object.keys(userInfo.knowledgeBaseGroupRoleInfos).length > 0)))"
            class="nav-tab" 
            :class="{ 'active': $route.path === '/knowledge' }"
            @click="navigateTo('/knowledge')"
          >
            <span class="tab-icon">📚</span>
            <span class="tab-text">资料库</span>
          </button>
          <button 
            v-if="hasPermission(['inventory']) || (userInfo && ((userInfo.researchRoleInfos && Object.keys(userInfo.researchRoleInfos).length > 0)))"
            class="nav-tab" 
            :class="{ 'active': $route.path === '/inventory' }"
            @click="navigateTo('/inventory')"
          >
            <span class="tab-icon">📦</span>
            <span class="tab-text">库存管理</span>
          </button>
        </div>
      </nav>

      <!-- 右侧功能区域 -->
      <div class="action-section" v-if="userNickname">
        <!-- 通知按钮 -->
        <button class="action-button notification-btn" @click="toggleNotificationModal">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"></path>
            <path d="M13.73 21a2 2 0 0 1-3.46 0"></path>
          </svg>
          <span v-if="unreadCount > 0" class="notification-badge">
            {{ unreadCount > 99 ? '99+' : unreadCount }}
          </span>
        </button>

        <!-- 用户信息 -->
        <div class="user-section" @click="toggleLogoutMenu">
          <div class="user-info">
            <div class="user-details">
              <span class="user-role">{{ userNickname }}</span>
              <span class="user-id">ID: {{ userPermissions[0] || 'N/A' }}</span>
            </div>
            <div class="user-avatar">
              <div class="avatar-text">
                {{ userNickname.charAt(0) }}
              </div>
            </div>
          </div>
        </div>

        <!-- 用户下拉菜单 -->
        <div v-if="showLogoutMenu" class="user-dropdown">
          <div class="dropdown-header">
            <div class="user-avatar-large">
              <img 
                src="/images/user-avatar.jpg" 
                alt="User Avatar" 
                class="avatar-image-large"
              />
            </div>
            <div class="user-info-detailed">
              <div class="user-name">{{ userNickname }}</div>
              <div class="user-role-text">超级管理员</div>
              <div class="user-id-text">ID: 001</div>
            </div>
          </div>
          
          <div class="dropdown-divider"></div>
          
          <div class="dropdown-actions">
            <button class="dropdown-item" @click="toggleNotificationModal">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9"></path>
                <path d="M13.73 21a2 2 0 0 1-3.46 0"></path>
              </svg>
              <span>消息通知</span>
              <span v-if="unreadCount > 0" class="dropdown-badge">
                {{ unreadCount > 99 ? '99+' : unreadCount }}
              </span>
            </button>
            
            <button class="dropdown-item logout-item" @click="handleLogout">
              <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"></path>
                <polyline points="16 17 21 12 16 7"></polyline>
                <line x1="21" y1="12" x2="9" y2="12"></line>
              </svg>
              <span>退出登录</span>
            </button>
          </div>
        </div>
      </div>
    </div>
  </header>

  <!-- 消息通知模态框 -->
  <MessageNotificationModal
    :show="showNotificationModal"
    :notifications="notifications"
    @close="showNotificationModal = false"
    @read="handleNotificationRead"
  />
</template>

<style scoped>
.app-header {
  position: relative;
  height: 60px;
  background: white;
  overflow: visible;
  transition: box-shadow 0.3s ease;
  border-bottom: 2px solid;
  border-image: linear-gradient(to right, #667eea, #764ba2) 1;
  box-shadow: 0 1px 3px rgba(102, 126, 234, 0.1);
}

.app-header:hover {
  box-shadow: 0 2px 16px rgba(102, 126, 234, 0.1);
}

/* 完美优化的粒子效果 */
.app-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: 
    radial-gradient(circle at 15% 25%, rgba(102, 126, 234, 0.015) 0%, transparent 1.2%),
    radial-gradient(circle at 85% 15%, rgba(118, 75, 162, 0.015) 0%, transparent 1.2%),
    radial-gradient(circle at 35% 65%, rgba(64, 224, 208, 0.015) 0%, transparent 1.2%),
    radial-gradient(circle at 65% 35%, rgba(255, 193, 7, 0.015) 0%, transparent 1.2%);
  background-size: 220% 220%;
  animation: particleMove 28s infinite cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 1;
  opacity: 0.5;
  will-change: background-position;
  pointer-events: none;
}

@keyframes particleMove {
  0% {
    background-position: 0% 0%;
  }
  25% {
    background-position: 80% 25%;
  }
  50% {
    background-position: 25% 80%;
  }
  75% {
    background-position: 80% 80%;
  }
  100% {
    background-position: 0% 0%;
  }
}

@media (max-width: 768px) {
  .app-header::before {
    background-size: 180% 180%;
    animation-duration: 22s;
    opacity: 0.3;
  }
  
  .app-header:hover {
    box-shadow: 0 1px 8px rgba(102, 126, 234, 0.05);
  }
}

@keyframes particleMove {
  0% {
    background-position: 0% 0%;
  }
  20% {
    background-position: 80% 20%;
  }
  40% {
    background-position: 20% 80%;
  }
  60% {
    background-position: 80% 80%;
  }
  80% {
    background-position: 20% 20%;
  }
  100% {
    background-position: 0% 0%;
  }
}

/* 优化内容区域 */
.header-content {
  position: relative;
  z-index: 2;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(2px);
}

@media (max-width: 768px) {
  .app-header::before {
    background-size: 200% 200%;
    animation-duration: 20s;
  }
  
  .header-content {
    backdrop-filter: none;
  }
}

.header-content {
  position: relative;
  z-index: 2;
  height: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* 品牌区域 */
.brand-section {
  display: flex;
  align-items: center;
  position: relative;
}

.brand-section::before {
  content: '';
  position: absolute;
  inset: 0;
  background: 
    radial-gradient(circle at 20% 35%, rgba(102, 126, 234, 0.05) 0%, transparent 3%),
    radial-gradient(circle at 75% 44%, rgba(118, 75, 162, 0.05) 0%, transparent 3%);
  background-size: 150% 150%;
  animation: particleMove 8s infinite;
  pointer-events: none;
}

@keyframes particleMove {
  0% {
    background-position: 0% 0%;
  }
  50% {
    background-position: 100% 75%;
  }
  100% {
    background-position: 0% 0%;
  }
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.logo-container:hover {
  transform: translateY(-1px);
}

.logo-icon {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to bottom right, #06b6d4, #3b82f6, #7c3aed);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  border: 4px solid rgba(255, 255, 255, 0.5);
  backdrop-filter: blur(4px);
  position: relative;
  overflow: hidden;
}

.logo-icon::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(
    45deg,
    transparent 20%,
    rgba(255, 255, 255, 0.1) 25%,
    transparent 30%
  );
  animation: shine 3s infinite;
}

.logo-icon::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(
    circle at 30% 30%,
    rgba(255, 255, 255, 0.2) 0%,
    transparent 60%
  );
}

@keyframes shine {
  0% {
    transform: translateX(-100%) rotate(45deg);
  }
  80%, 100% {
    transform: translateX(100%) rotate(45deg);
  }
}

.ai-text {
  font-size: 20px;
  font-weight: 900;
  color: white;
}

.brand-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.brand-title {
  font-size: 20px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0;
}

.version-tag {
  font-size: 12px;
  color: #666;
  font-weight: 500;
}

/* 导航区域 */
.main-navigation {
  flex: 1;
  display: flex;
  justify-content: center;
  max-width: 600px;
  margin: 0 40px;
}

.nav-tabs {
  display: flex;
  gap: 8px;
  background: rgba(255, 255, 255, 0.1);
  padding: 6px;
  border-radius: 16px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.nav-tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  background: linear-gradient(to right, #06b6d4, #3b82f6);
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
}

.nav-tab:hover {
  background: linear-gradient(to right, #06b6d4, #2563eb);
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(59, 130, 246, 0.3);
}

.nav-tab.active {
  background: linear-gradient(to right, #06b6d4, #1d4ed8);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

.tab-icon {
  font-size: 16px;
}

.tab-text {
  font-weight: 600;
}

/* 右侧功能区域 */
.action-section {
  display: flex;
  align-items: center;
  gap: 16px;
  position: relative;
}

.action-button {
  position: relative;
  width: 44px;
  height: 44px;
  background: rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #4a4a4a;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.action-button:hover {
  background: rgba(0, 0, 0, 0.1);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.notification-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  background: #ff4757;
  color: white;
  border-radius: 9px;
  font-size: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 用户区域 */
.user-section {
  position: relative;
  cursor: pointer;
  -webkit-text-size-adjust: 100%;
  tab-size: 4;
  font-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", Segoe UI Symbol, "Noto Color Emoji";
  font-feature-settings: normal;
  font-variation-settings: normal;
  -webkit-tap-highlight-color: transparent;
  --background: 0 0% 100%;
  --foreground: 222.2 84% 4.9%;
  --card: 0 0% 100%;
  --card-foreground: 222.2 84% 4.9%;
  --popover: 0 0% 100%;
  --popover-foreground: 222.2 84% 4.9%;
  --primary: 222.2 47.4% 11.2%;
  --primary-foreground: 210 40% 98%;
  --secondary: 210 40% 96.1%;
  --secondary-foreground: 222.2 47.4% 11.2%;
  --muted: 210 40% 96.1%;
  --muted-foreground: 215.4 16.3% 46.9%;
  --accent: 210 40% 96.1%;
  --accent-foreground: 222.2 47.4% 11.2%;
  --destructive: 0 84.2% 60.2%;
  --destructive-foreground: 210 40% 98%;
  --border: 214.3 31.8% 91.4%;
  --input: 214.3 31.8% 91.4%;
  --ring: 222.2 84% 4.9%;
  --radius: .5rem;
  --sidebar-background: 0 0% 98%;
  --sidebar-foreground: 240 5.3% 26.1%;
  --sidebar-primary: 240 5.9% 10%;
  --sidebar-primary-foreground: 0 0% 98%;
  --sidebar-accent: 240 4.8% 95.9%;
  --sidebar-accent-foreground: 240 5.9% 10%;
  --sidebar-border: 220 13% 91%;
  --sidebar-ring: 217.2 91.2% 59.8%;
  line-height: inherit;
  color: hsl(var(--foreground));
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  border-radius: 12px;
  border: 1px solid rgba(102, 126, 234, 0.2);
  background: rgba(255, 255, 255, 0.95);
  padding: 8px 16px;
  height: 44px;
  box-shadow: 
    0 2px 8px rgba(102, 126, 234, 0.1),
    0 0 0 1px rgba(255, 255, 255, 0.5) inset;
  backdrop-filter: blur(12px);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.user-info::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    135deg, 
    rgba(102, 126, 234, 0.05) 0%,
    rgba(118, 75, 162, 0.05) 100%
  );
  opacity: 0;
  transition: opacity 0.3s ease;
}

.user-info:hover {
  transform: translateY(-2px);
  box-shadow: 
    0 6px 16px rgba(102, 126, 234, 0.2),
    0 0 0 1px rgba(255, 255, 255, 0.8) inset;
  border-color: rgba(102, 126, 234, 0.4);
}

.user-info:hover::before {
  opacity: 1;
}

.user-info:active {
  transform: translateY(0);
  box-shadow: 
    0 2px 6px rgba(102, 126, 234, 0.2),
    0 0 0 1px rgba(255, 255, 255, 0.5) inset;
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.user-role {
  font-size: 14px;
  font-weight: 600;
  color: hsl(var(--primary));
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.user-id {
  font-size: 12px;
  color: hsl(var(--muted-foreground));
  font-weight: 500;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #6366f1 0%, #ec4899 100%);
  position: relative;
}

.user-avatar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.2) 0%, rgba(255, 255, 255, 0) 100%);
  border-radius: 50%;
}

.avatar-text {
  color: white;
  font-weight: 600;
  font-size: 18px;
  line-height: 1;
  position: relative;
  z-index: 1;
}

.user-info:hover .user-avatar {
  transform: scale(1.05);
  box-shadow: 0 6px 12px rgba(102, 126, 234, 0.3);
  border-color: rgba(255, 255, 255, 0.8);
}

.user-avatar:hover::before {
  opacity: 0.8;
}

.user-dropdown .user-avatar-large {
  transition: all 0.3s ease;
}

.user-dropdown .user-avatar-large:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 12px rgba(102, 126, 234, 0.3);
}

.user-info:hover .user-avatar {
  transform: scale(1.05);
}

.avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 用户下拉菜单 */
.user-dropdown {
  position: absolute;
  top: calc(100% + 12px);
  right: 0;
  width: 280px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 16px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  z-index: 1000;
  overflow: hidden;
  animation: dropdownSlideIn 0.3s ease;
}

@keyframes dropdownSlideIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-dropdown::before {
  content: '';
  position: absolute;
  top: -6px;
  right: 20px;
  width: 12px;
  height: 12px;
  background: rgba(255, 255, 255, 0.95);
  transform: rotate(45deg);
  border-left: 1px solid rgba(255, 255, 255, 0.3);
  border-top: 1px solid rgba(255, 255, 255, 0.3);
}

.dropdown-header {
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
}

.user-avatar-large {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid rgba(102, 126, 234, 0.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.avatar-image-large {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info-detailed {
  flex: 1;
}

.user-name {
  font-size: 16px;
  font-weight: 600;
  color: #2d3748;
  margin-bottom: 4px;
}

.user-role-text {
  font-size: 12px;
  color: #667eea;
  font-weight: 500;
  margin-bottom: 2px;
}

.user-id-text {
  font-size: 11px;
  color: #a0aec0;
}

.dropdown-divider {
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, rgba(0, 0, 0, 0.1) 50%, transparent 100%);
}

.dropdown-actions {
  padding: 12px;
}

.dropdown-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: #4a5568;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-bottom: 4px;
}

.dropdown-item:hover {
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
}

.dropdown-item.logout-item:hover {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
}

.dropdown-badge {
  margin-left: auto;
  min-width: 18px;
  height: 18px;
  padding: 0 6px;
  background: #ff4757;
  color: white;
  border-radius: 9px;
  font-size: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .header-content {
    padding: 0 16px;
  }
  
  .main-navigation {
    margin: 0 20px;
  }
  
  .nav-tab {
    padding: 10px 16px;
  }
  
  .tab-text {
    display: none;
  }
}

@media (max-width: 768px) {
  .app-header {
    height: 70px;
  }
  
  .brand-title {
    font-size: 18px;
  }
  
  .version-tag {
    font-size: 11px;
  }
  
  .main-navigation {
    margin: 0 12px;
  }
  
  .nav-tabs {
    gap: 4px;
    padding: 4px;
  }
  
  .nav-tab {
    padding: 8px 12px;
    font-size: 12px;
  }
  
  .user-details {
    display: none;
  }
  
  .user-dropdown {
    width: 260px;
    right: -20px;
  }
}

@media (max-width: 480px) {
  .header-content {
    padding: 0 12px;
  }
  
  .logo-icon {
    width: 40px;
    height: 40px;
  }
  
  .ai-text {
    font-size: 16px;
  }
  
  .brand-title {
    font-size: 16px;
  }
  
  .action-section {
    gap: 8px;
  }
  
  .action-button {
    width: 40px;
    height: 40px;
  }
}
</style>